package com.qf.web.controller;

import com.qf.domain.PageBean;
import com.qf.domain.Stu;
import com.qf.service.StuService;
import com.qf.service.impl.StuServiceImpl;
import com.qf.utils.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * projectName:lipengchong
 * author:LiPengChong
 * time:2024/4/30 15:25
 * description:
 */
@WebServlet("/showStu")
public class showStuServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        //获取前端页面数据
        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        int page_num=1;
        int page_size=8;
        try {
            //非空验证
            if (!StringUtils.isEmpty(pageNum)){
                page_num=Integer.parseInt(pageNum);
                if (page_num<1){
                    page_num=1;
                }
            }

            if (!StringUtils.isEmpty(pageSize)){
                page_size=Integer.parseInt(pageSize);
            }
        } catch (NumberFormatException e) {
            e.printStackTrace();
        }
        try {
            //调用业务逻辑
            StuService stuService = new StuServiceImpl();
            PageBean<Stu> pageBean =  stuService.findByPage(page_num,page_size);
            System.out.println("pageBean = " + pageBean);
            //转发重定向
            request.getSession().setAttribute("pageBean",pageBean);
            request.getRequestDispatcher("/showStu.jsp").forward(request,response);
        } catch (Exception e) {
            e.printStackTrace();
            request.setAttribute("msg", "查询失败" + e.getMessage());
            request.getRequestDispatcher("/adminIndex.jsp").forward(request,response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }
}


